package com.shyroke.coineye.service;

import com.shyroke.coineye.bean.AddCoinDto;
import com.shyroke.coineye.bean.ShowCoinData;
import com.shyroke.coineye.utils.CommonUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 爬虫Service
 */
public class CrawService {

    //存放想要爬取的币种的信息，即以太坊浏览器的地址
    private List<AddCoinDto> addCoinDtoList;

    public CrawService(List<AddCoinDto> addCoinDtoList){
        this.addCoinDtoList = addCoinDtoList;
    }

    /**
     * 解析
     * @return
     * @throws IOException
     */
    public List<ShowCoinData> parse() throws IOException {

        List<ShowCoinData> showCoinDataList = new ArrayList<>();

        URL url = null;
        ShowCoinData showCoinData = null;
        AddCoinDto addCoinDto = null;
        if(addCoinDtoList.size()>0){

            for(int i=0;i<addCoinDtoList.size();i++){
                addCoinDto = addCoinDtoList.get(i);
                url = new URL(addCoinDto.getUrl());
                Document document = Jsoup.parse(url,30000);

                if ( document!= null){
                    showCoinData = this.getInfo(document);
                    showCoinData.setCoinName(addCoinDto.getCoinName());
                    showCoinDataList.add(showCoinData);
                }

            }

        }

        return showCoinDataList;
    }

    /**
     * 获取所需要的信息
     * @param document
     * @return
     */
    private ShowCoinData getInfo(Document document) {
        ShowCoinData data = new ShowCoinData();
        //设置MTN总量
        Element supplyEle = document.getElementsByClass("tditem").get(0);

        //市值和总量
        String supply_value = supplyEle.text();

        //设置市值和总量
        Map<String,String> supplyValueMap = CommonUtils.splitBySupplyAndValue(supply_value);
        for(String key:supplyValueMap.keySet()){
            if("supply".equals(key)){
                data.setSupply(supplyValueMap.get(key));
            }else if("value".equals(key)){
                data.setValue(supplyValueMap.get(key));
            }
        }

        /**
         * 获取当前价格和跌涨幅
         */
        Element priceElement = document.getElementById("ContentPlaceHolder1_tr_valuepertoken");
       Element priceEle =  priceElement.getElementsByTag("td").get(1);

       if(priceEle!=null){
          Map<String,String> updownMap = CommonUtils.splitByPriceAndUpDown(priceEle.text());

          if(updownMap!=null){
              for(String key:updownMap.keySet()){
                  if("price".equals(key)){
                      data.setPrice(updownMap.get(key));
                  }else if("updown".equals(key)){
                      data.setUpdown(updownMap.get(key));
                  }
              }
          }

       }

        /**
         * 获取持有地址数
         */
        Element adderssElement = document.getElementById("ContentPlaceHolder1_tr_tokenHolders");
        Element addressEle =  adderssElement.getElementsByTag("td").get(1);
        data.setHolders(addressEle.text().replaceAll("addresses",""));

        return data;
    }


}
